package org.demos.server;

import java.util.UUID;

import org.demos.server.controller.RegisterServerController;
import org.demos.server.core.ServiceAliveMonitor;
import org.demos.server.pojo.HeartbeatRequest;
import org.demos.server.pojo.RegisterRequest;

/**
 * 代表了服务注册中心的这么一个东西
 * 这个组件应该是用来接受 register-client 发来的请求,然后将请求分发给相应处理接口
 * 在这里就模拟客户端发请求
 *
 * @author bing.zhang06@hand-china.com 2021/10/26 15:40
 **/
public class RegisterServer {
    public static void main(String[] args) {
        RegisterServerController controller = new RegisterServerController();

        String serviceInstanceId = UUID.randomUUID().toString().replace("-", "");

        // 模拟发起一个服务注册的请求
        RegisterRequest registerRequest = new RegisterRequest();
        registerRequest.setHostname("inventory-service-01");
        registerRequest.setIp("192.168.31.208");
        registerRequest.setPort(9000);
        registerRequest.setServiceInstanceId(serviceInstanceId);
        registerRequest.setServiceName("inventory-service");

        controller.register(registerRequest);

        // 模拟进行一次心跳，完成续约
        HeartbeatRequest heartbeatRequest = new HeartbeatRequest();
        heartbeatRequest.setServiceName("inventory-service");
        heartbeatRequest.setServiceInstanceId(serviceInstanceId);

        controller.heartbeat(heartbeatRequest);

        // 开启服务存活检测线程, 后台检测微服务存活状态
        new ServiceAliveMonitor().start();

        // 啥用意?
//        while(true) {
//            Thread.sleep(30 * 1000);
//        }
    }
}
